package com.ruoyi.web.controller.shop.service.Impl;

import com.ruoyi.common.entity.BO.wechat.AuthedAppletBO;
import com.ruoyi.common.entity.DO.shop.ShopAppletDO;
import com.ruoyi.common.entity.VO.shop.ShopAppletSearchVO;
import com.ruoyi.common.entity.VO.shop.ShopAppletVO;
import com.ruoyi.common.entity.VO.shop.ShopVO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.controller.shop.mapper.IBackShopAppletMapper;
import com.ruoyi.web.controller.shop.service.IBackShopAppletService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BackShopAppletServiceImpl implements IBackShopAppletService {

    @Resource
    private IBackShopAppletMapper backShopAppletMapper;

    @Override
    public List<ShopAppletVO> searchList(ShopAppletSearchVO shopAppletSearchVO) {
        shopAppletSearchVO.setIsDelete(0);
        return backShopAppletMapper.searchList(shopAppletSearchVO);
    }

    @Override
    public ShopAppletVO getShopAppletInfoById(Long shopAppletId) {
        return backShopAppletMapper.getShopAppletInfoById(shopAppletId);
    }

    @Override
    public ShopAppletDO getShopAppletInfoByShopId(Long shopId) {
        Example example = new Example(ShopAppletDO.class);
        example.createCriteria().andEqualTo("shopId", shopId);
        return backShopAppletMapper.selectOneByExample(example);
    }

    @Override
    public String checkShopAppletNameUnique(ShopAppletSearchVO shopAppletSearchVO) {
        return null;
    }

    @Override
    @Transactional
    public int insertShopApplet(ShopAppletVO shopAppletVO) {
        ShopAppletDO shopAppletDO = new ShopAppletDO();
        BeanUtils.copyProperties(shopAppletVO, shopAppletDO);
        return backShopAppletMapper.insertSelective(shopAppletDO);
    }

    @Override
    public int updateShopApplet(ShopAppletVO shopAppletVO) {
        ShopAppletDO shopAppletDO = backShopAppletMapper.selectByPrimaryKey(shopAppletVO.getId());
        if (shopAppletDO == null || shopAppletDO.getIsDelete() == 1) {
            throw new BusinessException("店铺小程序已被删除，请刷新后重试！");
        }
        BeanUtils.copyProperties(shopAppletVO, shopAppletDO);
        return backShopAppletMapper.updateByPrimaryKeySelective(shopAppletDO);
    }

    @Override
    public int deleteShopAppletById(Long id) {
        return 0;
    }
}
